# Create your views here.
from homecms.models import BlogEntry, QuickThought, Bookmark, Event
from django.shortcuts import render_to_response, get_object_or_404
from django.views.generic.list_detail import object_list, object_detail
from django.template import Context
from django.conf import settings
import feedparser
import datetime

MAX_INDEX_TAGS = 20

def index(request):
    Blogs = list(BlogEntry.objects.filter(active_after__lte=datetime.datetime.now())[:2])
    QTs = list(QuickThought.objects.all()[:2])
    Bookmarks = list(Bookmark.objects.all()[:2])
    Events = list(Event.objects.filter(start__lte=datetime.date.today())[:2])

    return render_to_response('index.html',{'latest_blogs':Blogs, 'latest_qts':QTs, 'latest_Bookmarks':Bookmarks, 'latest_events':Events})


def bookmarks(request):
    bms = [bm for bm in feedparser.parse('https://%s:%s@www.google.com/bookmarks/?output=rss'%(settings.HOMECMS_GOOGLE_USER, settings.HOMECMS_GOOGLE_PASSWORD))['entries'] if not bm.has_key('smh_bkmk_label') or "private" not in bm['smh_bkmk_label']]
    return render_to_response('surfing.html', Context({'bookmarks':bms}))

def history( request ):
    f = [h for h in feedparser.parse('https://%s:%s@www.google.com/history/?output=rss'%(settings.HOMECMS_GOOGLE_USER, settings.HOMECMS_GOOGLE_PASSWORD))['entries'] if 'web query' in [t['term'] for t in h['tags']] or 'web result' in [t['term'] for t in h['tags']]]
    return render_to_response('searching.html',Context({'history_list':f}))

def blog(request):
    L=BlogEntry.objects.filter(active_after__lt=datetime.datetime.now)
    return object_list(request, queryset=L, template_name='saying.html')

def entry_detail(request, entry_id):
    return object_detail(request, queryset=BlogEntry.objects.filter(active_after__lte=datetime.datetime.now()), object_id=entry_id, template_name='blogentry.html', template_object_name='entry')

def calendar(request):
    pass
